package com.hxkj.client.service.taglib.directive;

import com.hxkj.client.service.ArticleServiceClient;
import com.hxkj.domain.context.EnvironmentContextHolder;
import com.hxkj.domain.po.Article;
import com.hxkj.domain.po.WebsiteAddr;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Map;

/**
 * @Description 上一篇和下一篇文章
 * @Author chenfangzhou
 * @Date 2018/7/18 16:14
 */
@Component
@Slf4j
public class PreAndNextArticleDirective extends AbstractDirective {
    @Autowired
    private ArticleServiceClient articleServiceClient;
    //站点id
    private static final String SITE_ID = "siteId";
    //文章id
    private static final String ARTICLE_ID = "articleId";
    //分类id
    private static final String COLUMN_ID = "columnId";


    @Override
    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
        try {
            Long siteId = getLong(SITE_ID, params) == null ? (Long) getCustomAttrbute(env, SITE_ID) : getLong(SITE_ID, params);
            Long articleId = getLong(ARTICLE_ID, params) == null ? getArticleId(env) : getLong(ARTICLE_ID, params);
            Long columnId = getLong(COLUMN_ID, params) == null ? getColumnId(env) : getLong(COLUMN_ID, params);
            String note = super.getNote(env);
            Article preArticle;
            Article nextArticle;
            if (columnId == null || articleId == null) {
                log.debug("获取的分类id或文章id为空");
                preArticle = null;
                nextArticle = null;
            } else {
                Article article = new Article();
                article.setColumnId(columnId);
                article.setSiteId(siteId);
                article.setArticleId(articleId);
                preArticle = articleServiceClient.getPreArticle(article).getData();

                nextArticle = articleServiceClient.getNextArticle(article).getData();

                WebsiteAddr webSiteAddr = getWebSiteAddr(siteId);
                if (StringUtils.isNotBlank(note)) {
                    if (preArticle != null) {
                        preArticle.setManuscriptPath(webSiteAddr.getIpAdd() + preArticle.getManuscriptPath());
                    }
                    if (nextArticle != null) {
                        nextArticle.setManuscriptPath(webSiteAddr.getIpAdd() + nextArticle.getManuscriptPath());
                    }
                } else {
                    Map<String, Object> enviroment = EnvironmentContextHolder.getEnvironment();
                    Map<Long, String> siteCtxMap = (Map<Long, String>) enviroment.get("siteCtxMap");
                    String urlContext = siteCtxMap.get(siteId);
                    // 静态文件翻页链接加前缀
                    if(StringUtils.isNotBlank(urlContext)) {
                        if (preArticle != null) {
                            preArticle.setManuscriptPath(urlContext + preArticle.getManuscriptPath());
                        }
                        if (nextArticle != null) {
                            nextArticle.setManuscriptPath(urlContext + nextArticle.getManuscriptPath());
                        }
                    }
                }

            }


            env.setVariable("preArticle", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(preArticle));
            env.setVariable("nextArticle", DefaultObjectWrapperBuilderFactory.getDefaultObjectWrapper().wrap(nextArticle));

            if (body != null) {
                body.render(env.getOut());
            } else {
                log.debug("文章上下篇所在模板至少要加一个空格");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
